package com.zbkj.service.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.dal.dao.ProductTabMapper;
import com.zbkj.dal.entity.product.ProductTab;
import com.zbkj.dal.entity.product.ProductTabRelation;
import com.zbkj.dal.pojo.request.ba.TabProductRequest;
import com.zbkj.dal.pojo.response.ProductFrontResponse;
import com.zbkj.dal.dao.ProductDao;
import com.zbkj.dal.dao.ProductTabRelationMapper;
import com.zbkj.service.service.ProductTabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品与tab关系表 服务实现类
 * </p>
 *
 * @author jhui
 * @since 2023-12-03
 */
@Service
public class ProductTabServiceImpl implements ProductTabService {

    @Autowired
    private ProductDao productDao;
    @Autowired
    private ProductTabMapper productTabMapper;
    @Autowired
    private ProductTabRelationMapper productTabRelationMapper;

    @Override
    public List<ProductTab> getAllEnableProductTabs(Integer enable) {
        LambdaQueryWrapper<ProductTab> lambdaQuery = Wrappers.lambdaQuery();
        if (Objects.isNull(enable)) {
            lambdaQuery.eq(ProductTab::getEnable, 1);
        } else if (!Objects.equals(enable, -1)) {
            lambdaQuery.eq(ProductTab::getEnable, enable);
        }
        lambdaQuery.orderByAsc(ProductTab::getSort);
        return productTabMapper.selectList(lambdaQuery);
    }

    @Override
    public void deleteByProductId(Integer productId) {
        LambdaQueryWrapper<ProductTabRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductTabRelation::getProductId, productId);
        productTabRelationMapper.delete(lqw);
    }

    @Override
    public List<ProductTabRelation> listByProductId(Integer productId) {
        LambdaQueryWrapper<ProductTabRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductTabRelation::getProductId, productId);
        return productTabRelationMapper.selectList(lqw);
    }

    @Override
    public void saveProductTabRelation(Integer productId, List<String> tabCodes) {
        LambdaQueryWrapper<ProductTab> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.in(ProductTab::getTabCode, tabCodes);
        List<ProductTab> productTabs = productTabMapper.selectList(lambdaQuery);
        productTabs.forEach(productTab -> {
            ProductTabRelation productTabRelation = new ProductTabRelation();
            productTabRelation.setProductId(productId);
            productTabRelation.setTabCode(productTab.getTabCode());
            productTabRelationMapper.insert(productTabRelation);
        });
    }

    @Override
    public List<ProductFrontResponse> listTabProducts(TabProductRequest tabProductRequest) {
        return productDao.listTabProducts(tabProductRequest);
    }

    @Override
    public void addProductTab(String name, Integer sort, Integer enable) {
        ProductTab productTab = new ProductTab();
        productTab.setTabCode(IdUtil.getSnowflakeNextIdStr());
        productTab.setTabName(name);
        productTab.setSort(sort);
        productTab.setEnable(enable);
        productTabMapper.insert(productTab);
    }

    @Override
    public void modifyProductTab(String code, String name, Integer sort, Integer enable) {
        LambdaQueryWrapper<ProductTab> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(ProductTab::getTabCode, code);
        ProductTab productTab = productTabMapper.selectOne(lambdaQuery);
        Assert.notNull(productTab, "商品Tab不存在");
        productTab.setTabName(name);
        productTab.setSort(sort);
        productTab.setEnable(enable);
        productTabMapper.updateById(productTab);
    }

    @Override
    public List<ProductTab> findProductTabByViewIn(List<String> view) {
        LambdaQueryWrapper<ProductTab> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.in(ProductTab::getView, view);
        return productTabMapper.selectList(lambdaQuery);
    }
}
